/*
 * tangoc.c
 *
 *  Created on: 2012-6-8
 *      Author: Administrator
 */

#include "tangoc.h"
#include <time.h>

void msleep(unsigned int n)
{
	struct timeval delay;
	delay.tv_sec = 0;
	delay.tv_usec = 1000 * n;
	select(0, NULL, NULL, NULL, &delay);
}

void cat_time(char *buf, int time)
{
	char tmp[8];
	time < 10 ? sprintf(tmp, "0%d", time) : sprintf(tmp, "%d", time);
	strcat(buf, tmp);
}

void get_time(int mode, char *buf)
{
	time_t now;
	struct tm *ptm;
	int y, m, d, h, n, s;

	time(&now);
	ptm = localtime(&now);
	y = ptm->tm_year + 1900; //年
	m = ptm->tm_mon + 1; //月
	d = ptm->tm_mday; //日
	h = ptm->tm_hour; //时
	n = ptm->tm_min; //分
	s = ptm->tm_sec; //秒

	sprintf(buf, "%d", y);
	cat_time(buf, m);
	cat_time(buf, d);
	cat_time(buf, h);
	cat_time(buf, n);
	cat_time(buf, s);
	switch (mode)
	{
	case DEBUG_ORIGIN_MODE:
		strcat(buf, ".dbg");
		break;
	case DEBUG_EEPROM_OFFSET:
		strcat(buf, ".dep");
		break;
	case DEBUG_RAM_OFFSET:
		strcat(buf, ".drm");
		break;
	case RASTER_ORIGIN_MODE:
		strcat(buf, ".rst");
		break;
	case RASTER_EEPROM_OFFSET:
		strcat(buf, ".rep");
		break;
	case RASTER_LX_SY:
		strcat(buf, ".tr1");
		break;
	case RASTER_LX_SY_OFFSET:
		strcat(buf, ".tr2");
		break;
	case RASTER_LY_SX:
		strcat(buf, ".tr3");
		break;
	case RASTER_LY_SX_OFFSET:
		strcat(buf, ".tr4");
		break;
	case TUNE_DEBUG:
		strcat(buf, ".tdb");
		break;
	default:
		strcat(buf, ".unk"); // unknown
		break;
	}
}

void finish()
{
	close(gdev);
	exit(0);
}


